package day2.homework;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

//统计一个文本文件中各个字母出现次数：
//* A(8),B(16),C(10)...,a(12),b(10),c(3)....，括号内代表字符出现次数;
//统计结束后写入到一个result.txt中显示结果
public class Work6 {
    public static void main(String[] args) {
        fun3();
    }
    public static void fun1(){
        //定义一个数组 存放ascii值 的次数
        int[] asc = new int[128];
        try (
                FileInputStream fis = new FileInputStream("d://IO//testAscii.txt")
        ){
            int len = 0;
            byte[] bs = new byte[1024];
            while ((len = fis.read(bs)) != -1){
                String str = new String(bs,0,len);
                System.out.println(str);
                for (int i = 0; i < str.length(); i++) {
                    char c = str.charAt(i);
                    asc[c] ++;
                }
            }
            for (int i = 65; i < asc.length-1; i++) {
                if ((i<=90)||(97<=i&&i<=122)){
                    System.out.print((char) i+"("+asc[i]+") ");
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void fun2(){
        try (
             FileOutputStream fos = new FileOutputStream("d://IO//result.txt");
             FileInputStream fis = new FileInputStream("d://IO//testAscii.txt");
        ){
            //统计每个字符次数:map集合(key(char)-value)
            Map<Character,Integer> map = new HashMap<>();
            int len = 0;
            byte[] bs = new byte[1024];
            while ((len = fis.read(bs)) != -1){
                String str = new String(bs,0,len);
                for (int i = 0; i < str.length(); i++) {
                    char c = str.charAt(i);
                    //如果key存在 次数+1
                    if (map.containsKey(c)) map.put(c,map.get(c)+1);
                    //key不存在 次数=1
                    else map.put(c,1);
                }
            }
            //处理写入的 结果字符串
            StringBuilder sb = new StringBuilder("最终的结果是:字符(次数)\n");
            for (Character key:map.keySet()){
                Integer value = map.get(key);
                sb.append(key+"("+value+")\n");
            }
            //写入结果字符串:
            fos.write(sb.toString().getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void fun3(){
        try (
             FileReader fr = new FileReader("d://IO//testAscii.txt");
             FileWriter fw = new FileWriter("d://IO//result.txt")
        ){
            //统计每个字符次数:map集合(key(char)-value)
            Map<Character,Integer> map = new HashMap<>();
            int len = 0;
            char[] ch = new char[1024];
            while ((len = fr.read(ch)) != -1){
                String str = new String(ch,0,len);
                for (int i = 0; i < str.length(); i++) {
                    char c = str.charAt(i);
                    //如果key存在 次数+1
                    if (map.containsKey(c)) map.put(c,map.get(c)+1);
                        //key不存在 次数=1
                    else map.put(c,1);
                }
            }
            //处理写入的 结果字符串
            StringBuilder sb = new StringBuilder("最终的结果是:字符(次数)\n");
            for (Character key:map.keySet()){
                Integer value = map.get(key);
                sb.append(key+"("+value+")\n");
            }
            //写入结果字符串:
            fw.write(sb.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
